/*
 * display_main.cpp
 *
 *  Created on: 21-Jun-2009
 *      Author: bob
 */

/*
 * Acorn-SDR ('A Collection of Radio Nodes')
 * (c) Bob Cowdery (G3UKB) 2009
 *
 * File: display_main.cpp
 * Date: June 2009
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * The author can be reached by email at
 *
 * bob@g3ukb.co.uk
 *
 * ================================================================================
 * Module Description:
 *
 * Main path for the displays node. The display node presents the incoming data as
 * real-time graphs and levels.
*/


#include <Ice/Ice.h>
#include <IceStorm/IceStorm.h>
#include <typeinfo>
#include "displays.h"
#include "persist.h"

using namespace std;
using namespace AcornDisplays;
using namespace AcornPersist;

union PERSIST    // Declare union type for persistent data
{
    int    	i;
    float  	f;
    char 	s[256];
} persist_union;

//=================================================================================
// Utility functions here for now
//
PERSIST persist_get(PersistPrx persist_ref, string category, string key, int radio=0) {
	//object o = persist_ref.get(category, key, radio);
	::Ice::Context* ice;
	if (typeid(persist_ref.get(category, key, radio, ice)) == typeid(AcornPersist.MemberInt))
		persist_union.i = (integer)o.i;
	else if (typeid(persist_ref.get(category, key, radio, ice)) == typeid(AcornPersist.MemberFloat))
		persist_union.f = (float)o.f;
	else if (typeid(persist_ref.get(category, key, radio, ice)) == typeid(AcornPersist.MemberString))
		persist_union.s = (string)o.s;
	else
		persist_union.s = "";

	return persist_union;
}

//=================================================================================
// Receiver for display data
//
class DisplaysI : public Displays {

public:
	virtual void data(const displaydata& display, const Ice::Current&);
};

void DisplaysI::data(const displaydata& display, const Ice::Current&) {
	cout << "Got some data!\n";
}

//=================================================================================
// Entry point
//
int main(int argc, char* argv[])
{

	int status = 0;

	Ice::CommunicatorPtr ic;
	Ice::ObjectPrx base;
	PersistPrx persist;
	IceStorm::TopicManagerPrx topicManager;
	Ice::ObjectPrx proxy;
	Ice::ObjectAdapterPtr adapter;
	IceStorm::TopicPrx topic;

	// Initialise the Ice system
	try {
			ic = Ice::initialize(argc, argv);
		} catch (const Ice::Exception& e) {
			cerr << e << endl;
			status = 1;
			exit (1);
		} catch (const char* msg) {
			cerr << msg << endl;
			status = 1;
			exit (1);
		}

		// get a proxy for the persist interface
	try {
		base = ic->propertyToProxy("Persist.Proxy");
		persist = PersistPrx::checkedCast(base);
		if (!persist)
			throw "Invalid proxy";
		} catch (const Ice::Exception& ex) {
			cerr << ex << endl;
			status = 1;
		} catch (const char* msg) {
			cerr << msg << endl;
			status = 1;
		}

		cout << "Path = " << persist_get(persist, 'Profile', 'path.glade');

	try {
		Ice::ObjectPrx obj = ic->propertyToProxy("TopicManager.Proxy");
		topicManager = IceStorm::TopicManagerPrx::checkedCast(obj);
		adapter = ic->createObjectAdapter("Display.Subscriber");
		DisplaysPtr display = new DisplaysI;
		proxy = adapter->addWithUUID(display)->ice_oneway();
	} catch (const Ice::Exception& e) {
		cerr << e << endl;
		status = 1;
		exit (1);
	} catch (const char* msg) {
		cerr << msg << endl;
		status = 1;
		exit (1);
	}

	try {
		topic = topicManager->retrieve("display_events");
		IceStorm::QoS qos;
		topic->subscribeAndGetPublisher(qos, proxy);
	}
	catch (const IceStorm::NoSuchTopic&) {
		cout << "Error! No topic found!";
		exit(1);
	}

	try {
		adapter->activate();
	} catch (const Ice::Exception& e) {
		cerr << e << endl;
		status = 1;
	}
	cout << "Adapter activated\n";
	ic->waitForShutdown();
	topic->unsubscribe(proxy);

	return status;
}

